from rest_framework import serializers

from city.models import China
from motion.serializer import InvolverMotionPathSerializer
from report.models import InvolverInfo, Party, MigrantsInfo


class InvolverInfoSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    motion_path = serializers.SerializerMethodField()

    def get_motion_path(self, obj):

        serializer = InvolverMotionPathSerializer(obj.motion_path.filter(delete_flag=False), many=True, context={'request': self.context['request']})
        # serializer = InvolverMotionPathTimeRangeSerializer(obj.motion_path, many=True, context={'request': self.context['request']})
        return serializer.data

    class Meta:
        model = InvolverInfo
        fields = "__all__"


class MigrantsInfoSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    party = serializers.SerializerMethodField()
    # from_label1 = serializers.SerializerMethodField()
    # to_label1 = serializers.SerializerMethodField()
    #
    # def get_to_label1(self, obj):
    #     try:
    #         province_name = ""
    #         city_name = ""
    #         area_name = ""
    #         if obj.to_province_id:
    #             province = China.objects.get(id=obj.to_province_id)
    #             province_name = province.name
    #         if obj.to_city_id:
    #             city = China.objects.get(id=obj.to_city_id)
    #             city_name = city.name
    #         if obj.to_area_id:
    #             area = China.objects.get(id=obj.to_area_id)
    #             area_name = area.name
    #         name_ = "%s-%s-%s" % (province_name, city_name, area_name)
    #         obj.to_label = name_
    #         obj.save()
    #         return name_
    #     except Exception as e:
    #         print(e.args)
    #         obj.to_label = ''
    #         obj.save()
    #         return ""
    #
    # def get_from_label1(self, obj):
    #     try:
    #         province_name = ""
    #         city_name = ""
    #         area_name = ""
    #         if obj.from_province_id:
    #             province = China.objects.get(id=obj.from_province_id)
    #             province_name = province.name
    #         if obj.from_city_id:
    #             city = China.objects.get(id=obj.from_city_id)
    #             city_name = city.name
    #         if obj.from_area_id:
    #             area = China.objects.get(id=obj.from_area_id)
    #             area_name = area.name
    #         name_ = "%s-%s-%s" % (province_name, city_name, area_name)
    #         obj.from_label = name_
    #         obj.save()
    #         return name_
    #     except Exception as e:
    #         print(e.args)
    #         obj.from_label = ''
    #         return ""

    def get_party(self, obj):
        serializer = PartySerializer(obj.party.filter(delete_flag=False), many=True, context={'request': self.context['request']})
        return serializer.data

    class Meta:
        model = MigrantsInfo
        fields = "__all__"


class PartySerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    # from_label1 = serializers.SerializerMethodField()
    # to_label1 = serializers.SerializerMethodField()
    #
    # def get_to_label1(self, obj):
    #     try:
    #         province_name = ""
    #         city_name = ""
    #         area_name = ""
    #         if obj.to_province_id:
    #             province = China.objects.get(id=obj.to_province_id)
    #             province_name = province.name
    #         if obj.to_city_id:
    #             city = China.objects.get(id=obj.to_city_id)
    #             city_name = city.name
    #         if obj.to_area_id:
    #             area = China.objects.get(id=obj.to_area_id)
    #             area_name = area.name
    #         name_ = "%s-%s-%s" % (province_name, city_name, area_name)
    #         obj.to_label = name_
    #         obj.save()
    #         return name_
    #     except Exception as e:
    #         print(e.args)
    #         obj.to_label = ''
    #         obj.save()
    #         return ""
    #
    # def get_from_label1(self, obj):
    #     try:
    #         province_name = ""
    #         city_name = ""
    #         area_name = ""
    #         if obj.from_province_id:
    #             province = China.objects.get(id=obj.from_province_id)
    #             province_name = province.name
    #         if obj.from_city_id:
    #             city = China.objects.get(id=obj.from_city_id)
    #             city_name = city.name
    #         if obj.from_area_id:
    #             area = China.objects.get(id=obj.from_area_id)
    #             area_name = area.name
    #         name_ = "%s-%s-%s" % (province_name, city_name, area_name)
    #         obj.from_label = name_
    #         obj.save()
    #         return name_
    #     except Exception as e:
    #         print(e.args)
    #         obj.from_label = ''
    #         return ""

    class Meta:
        model = Party
        fields = "__all__"
